package com.qf.sl.youshare.adapters;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.qf.sl.youshare.Constants;
import com.qf.sl.youshare.R;
import com.qf.sl.youshare.model.Product;
import com.qf.sl.youshare.utils.FileCache;
import com.qf.sl.youshare.utils.HttpTools;
import com.qf.sl.youshare.utils.ImageCache;
import com.qf.sl.youshare.utils.ImageLoader;

import java.util.List;

/**
 * Created by IDEA
 * User : sl
 * on 2015/2/6.
 * 11:40
 */

/**
 * 生成核心产品列表 ListView 的 Adapter
 */
public class StoreCoreAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private List<Product> products;

    public StoreCoreAdapter(Context context, List<Product> products) {
        if (context != null) {
            inflater = LayoutInflater.from(context);
        }else {
            throw new IllegalArgumentException("Context must not null");
        }
        this.products = products;
    }

    @Override
    public int getCount() {
        int ret = 0;
        if (products != null) {
            ret = products.size();
        }
        return ret;
    }

    @Override
    public Object getItem(int position) {
        Object ret = null;
        if (products != null) {
            ret = products.get(position);
        }
        return ret;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View ret = null;

        //convertView 用于复用, convertView 就是ListView 滚动的时候, 滑出去的View
        if (convertView != null) {
            ret = convertView;
        }else {
            ret = inflater.inflate(R.layout.item_store_core,parent,false);
        }

        ret.setBackgroundColor(Constants.COLORS[position % Constants.COLORS.length]);

        ViewHolder holder = (ViewHolder) ret.getTag();
        if (holder == null) {
            holder = new ViewHolder();

            holder.positionHolder = (TextView) ret.findViewById(R.id.item_core_position);
            holder.shortTitleHolder = (TextView) ret.findViewById(R.id.item_core_short_title);
            holder.titleHolder = (TextView) ret.findViewById(R.id.item_core_title);
            holder.iconHolder = (ImageView) ret.findViewById(R.id.item_core_product_icon);

            ret.setTag(holder);
        }

        Product product = products.get(position);

        String appGname = product.getAppGname();
        if (appGname == null || appGname.length() == 0) {
            appGname = product.getGoodsName();
        }

        String name = product.getGoodsName();

        String string = null;

        position++;

        if (position < 10){
            string = "0"+ position;
        }else {
            string = position + "";
        }

        //显示索引位置
        holder.positionHolder.setText(string);
        holder.shortTitleHolder.setText(appGname);
        holder.titleHolder.setText(name);

        //TODO 加载图片

//        final ImageView myImageView = holder.iconHolder;

        String goodsThumb = product.getGoodsThumb();

        holder.iconHolder.setTag(Constants.IMAGE_SERVER + goodsThumb);



        /*
            获取缩略图,缩略图采用的是相对路径的形式,
            要想完成访问,必须要进行网址的处理
         */

        if (goodsThumb != null && goodsThumb.length() > 0) {
            if (!goodsThumb.startsWith("http://") && !goodsThumb.startsWith("https://")) {
                //TODO 进行网址的添加
                goodsThumb= Constants.IMAGE_SERVER + goodsThumb;
            }
            //设置默认图片,避免出现复用的View显示以前图片的问题
            holder.iconHolder.setImageResource(R.drawable.product_default);

            ImageCache imageCache = ImageCache.getInstance();
            Bitmap bitmap = imageCache.getImage(goodsThumb);

            if (bitmap != null) {

                holder.iconHolder.setImageBitmap(bitmap);
            }else {
                //不包含缓存,就进行本地文件的加载

                //TODO 加载本地文件内容

                FileCache fileCache = FileCache.getInstance();
                byte[] bytes = fileCache.getFileContent(goodsThumb);
                if (bytes != null && bytes.length > 0) {
                    Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
                    holder.iconHolder.setImageBitmap(bmp);

                    imageCache.putImage(goodsThumb,bmp);
                }else {

                    //进行网络的加载
                    // 1,开启异步任务

                    ImageLoader task = new ImageLoader(holder.iconHolder);
                    task.execute(goodsThumb);
                }
            }
        }

        return ret;
    }


    /**
     * 避免每次进行 findViewById
     */
    private class ViewHolder{
        public TextView positionHolder;
        public TextView shortTitleHolder;
        public TextView titleHolder;
        public ImageView iconHolder;
    }
}
